﻿Ext.define('page.Collection.Mod', {
    extend: 'Kwan.ex.ModuleInstance',
    metas: [
        { name: "BillNO", label: "账单号", sort: 1, ex_list: { width: 170} },
        { name: "OrderID", label: "订单号", sort: 2, ex_list: { width: 150} },
        { name: "RoomNO", label: "房间号", sort: 3, ex_list: { width: 100} },
        { name: "Received", label: "已收押金", sort: 4, ex_list: { width: 100} },
        { name: "Receivable", label: "应收金额", sort: 5, ex_list: { width: 100} },
        { name: "Paid", label: "实收金额", sort: 6, ex_list: { width: 100} },
        { name: "Change", label: "找零", sort: 7, ex_list: { width: 100} },
        { name: "CashPay", label: "现金支付", sort: 8, ex_list: { width: 100} },
        { name: "PosPay", label: "银行卡支付", sort: 8, ex_list: { width: 100} },
        { name: "Invoice", label: "发票金额", sort: 8, ex_list: { width: 100} },
        { name: "InvoiceNO", label: "发票号码", sort: 8, ex_list: { width: 100} },
        { name: "Status", label: "状态", sort: 9, ex_list: { width: 100, renderer: Kwan.colorCsStatus} },
        { name: "UserName", label: "经手人", sort: 8, ex_list: { width: 80} },
        { name: "CreatedDate", label: "收款日期", sort: 8, ex_list: { width: 180, xtype: 'datecolumn', format: 'Y-m-d H:i:s'} }
	    ],
    cfg: { start: 0, limit: 20, facade: 'facade.axd?s_action=Collection', def: 'Collection' },
    getGrid: function () {
        var me = this;
        Ext.apply(Ext.form.field.VTypes, {
            daterange: function (val, field) {
                var date = field.parseDate(val);

                if (!date) {
                    return false;
                }
                if (field.startDateField && (!this.dateRangeMax || (date.getTime() != this.dateRangeMax.getTime()))) {
                    var start = Ext.getCmp(field.startDateField);
                    start.setMaxValue(date);
                    start.validate();
                    this.dateRangeMax = date;
                }
                else if (field.endDateField && (!this.dateRangeMin || (date.getTime() != this.dateRangeMin.getTime()))) {
                    var end = Ext.getCmp(field.endDateField);
                    end.setMinValue(date);
                    end.validate();
                    this.dateRangeMin = date;
                }
                return true;
            },
            daterangeText: 'Start date must be less than end date'
        });
        var ds = Ext.create('Ext.data.Store', {
            autoLoad: true,
            pageSize: me.cfg.limit,
            remoteSort: true,
            remoteFilter: true,
            groupField: 'OrderID',
            fields: Kwan.createModel(me.metas),
            sorters: [{ property: 'CreatedDate', direction: 'ASC'}],
            proxy: { type: 'ajax', url: me.cfg.facade + '&s_method=list', reader: { type: 'json', root: 'data.data', totalProperty: 'data.total' }
            }
        });
        var groupingFeature = Ext.create('Ext.grid.feature.GroupingSummary', {
            groupHeaderTpl: '订单号：{name} (共{[values.rows.length]}项)', hideGroupedHeader: true
        });
        var grid = Ext.create('Kwan.ex.Grid', {
            id: 'gpCollection',
            region: 'center',
            store: ds,
            columns: Kwan.createColumn(this.metas, true),
            features: [{
                ftype: 'groupingsummary',
                hideGroupedHeader: true,
                groupHeaderTpl: '订单号：{name} (共{[values.rows.length]}项)'
            }],
            getQuery: function (clear) {
                var roomNO = Ext.getCmp('qRoomNO').getValue(),
                    startDate = Ext.util.Format.date(Ext.getCmp('qStartDate').getValue(), 'Y-m-d'),
                    endDate = Ext.util.Format.date(Ext.getCmp('qEndDate').getValue(), 'Y-m-d');
                if (clear) {
                    startDate = endDate = roomNO = '';
                }
                if (roomNO.length > 0 || startDate.length > 0 || endDate.length > 0) {
                    var val = "";
                    val += (roomNO.length > 0 ? roomNO : "") + ",";
                    val += (startDate.length > 0 ? startDate : "") + ",";
                    val += (endDate.length > 0 ? endDate : "") + ",";
                    this.store.proxy.extraParams = { query1: 'tag=' + val };
                } else { this.store.proxy.extraParams = null; }
                this.store.load();
                this.filters.bindStore(this.store);
            },
            getDateStr: function (AddDayCount) {
                var dd = new Date();
                dd.setDate(dd.getDate() + AddDayCount);
                var y = dd.getYear();
                var m = dd.getMonth() + 1;
                var d = dd.getDate();
                return y + "-" + m + "-" + d;
            },
            operBar: [
                    { xtype: 'hidden', id: 'qOrderID', name: 'qOrderID' },
                    { width: 100, emptyText: " 搜索房间号..", xtype: 'textfield', id: 'qRoomNO', name: 'qRoomNO' }, '-',
                    { xtype: 'datefield', width: 120, emptyText: '开始日期', id: 'qStartDate', name: 'qStartDate', vtype: 'daterange', endDateField: 'qEndDate', format: 'Y年m月d日',
                        listeners: { afterrender: function () { this.setValue(new Date(Date.parse(new Date()) - (86400000 * 1))); } }
                    },
                    { xtype: 'datefield', width: 120, emptyText: '结束日期', id: 'qEndDate', name: 'qEndDate', vtype: 'daterange', startDateField: 'qStartDate', format: 'Y年m月d日',
                        listeners: { afterrender: function () { this.setValue(new Date(Date.parse(new Date()) - (86400000 * 1))); } }
                    }, '-',
                    { scale: 'medium', text: '搜索', icon: "/Admin/style/24Icons/jiancha.png", name: 'btnSearch',
                        handler: function () { Ext.getCmp('gpCollection').getQuery(); }
                    }
                ]

        });

        return grid;
    },
    run: function () {
        var me = this;
        me.center = me.getGrid();
        me.viewport.add([me.center]);
    }
});